import { useUser } from "@/stores/useUser";
import { conversionMessage } from "./helpers/message-converter";
import { messageHandler } from "./helpers/message-handler";
import type {
	AddBuckleCardsMessageData,
	AddDelayedCardsMessageData,
	AddMaterielCardsMessageData,
	CreateRoomMessageData,
	GetDiscardCardsMessageData,
	GetDrawCardsMessageData,
	GetPlayerCardsMessageData,
	JoinRoomMessageData,
	LeaveRoomMessageData,
	SetDeadMessageData,
	SetFlippedMessageData,
	SetHealthLimitMessageData,
	SetHealthMessageData,
	SetHerosMessageData,
	SetHiddenHerosMessageData,
	SetSkillsMessageData,
	SetTiedMessageData,
	ShuffleMessageData,
	SpeakMessageData,
	StartGameMessageData,
	TransferDelayedCardsToPlayerDelayedCardsMessageData,
	TransferDiscardCardsToDiscardCardsMessageData,
	TransferDiscardCardsToDrawCardsMessageData,
	TransferDrawCardsToDiscardCardsMessageData,
	TransferDrawCardsToDrawCardsMessageData,
	TransferPlayerCardsToDiscardCardsMessageData,
	TransferPlayerCardsToDrawCardsMessageData,
	TransferPlayerCardsToPlayerHandCardsMessageData,
	ViewDiscardCardsMessageData,
	ViewDrawCardsMessageData,
	ViewPlayerCardsMessageData,
} from "./types/MessageData";
import { SOCKET_URL } from "@/configs/yz";

let ws = null as WebSocket | null;
let timer = null as number | null;
let count = 0;
export const initSocket = () => {
	const userStore = useUser();
	ws = new WebSocket(SOCKET_URL + "?token=" + userStore.token);
	ws.onmessage = (message) => {
		count = 0;
		messageHandler(message.data);
	};
	if (timer) clearInterval(timer);
	timer = setInterval(() => {
		count += 1;
		if (count % 10 === 0) {
			socketPing();
		}
		if (count % 15 === 0) {
			if (timer) clearInterval(timer);
			ws?.close();
			initSocket();
		}
	}, 1000);
};
export const onSocketInited = () => {
	return new Promise((resolve, reject) => {
		let count = 0;
		fn();
		function fn() {
			count++;
			if (ws && ws.readyState == ws.OPEN) resolve(null);
			else if (count > 10) reject(null);
			else setTimeout(() => fn(), 500);
		}
	});
};
export const socketPing = () => ws?.send(conversionMessage({ type: "ping", payload: null }));
export const socketSelectRoomList = () => ws?.send(conversionMessage({ type: "selectRoomList", payload: null }));
export const socketCreateRoom = (payload: CreateRoomMessageData["payload"]) => ws?.send(conversionMessage({ type: "createRoom", payload }));
export const socketJoinRoom = (payload: JoinRoomMessageData["payload"]) => ws?.send(conversionMessage({ type: "joinRoom", payload }));
export const socketLeaveRoom = (payload: LeaveRoomMessageData["payload"]) => ws?.send(conversionMessage({ type: "leaveRoom", payload }));
export const socketStartGame = (payload: StartGameMessageData["payload"]) => ws?.send(conversionMessage({ type: "startGame", payload }));
export const socketGetRoomState = () => ws?.send(conversionMessage({ type: "getRoomState", payload: null }));
export const socketSpeak = (payload: SpeakMessageData["payload"]) => ws?.send(conversionMessage({ type: "speak", payload }));
export const socketSetHeros = (payload: SetHerosMessageData["payload"]) => ws?.send(conversionMessage({ type: "setHeros", payload }));
export const socketSetHiddenHeros = (payload: SetHiddenHerosMessageData["payload"]) => ws?.send(conversionMessage({ type: "setHiddenHeros", payload }));
export const socketSetFlipped = (payload: SetFlippedMessageData["payload"]) => ws?.send(conversionMessage({ type: "setFlipped", payload }));
export const socketSetHealthLimit = (payload: SetHealthLimitMessageData["payload"]) => ws?.send(conversionMessage({ type: "setHealthLimit", payload }));
export const socketSetHealth = (payload: SetHealthMessageData["payload"]) => ws?.send(conversionMessage({ type: "setHealth", payload }));
export const socketSetTied = (payload: SetTiedMessageData["payload"]) => ws?.send(conversionMessage({ type: "setTied", payload }));
export const socketSetSkills = (payload: SetSkillsMessageData["payload"]) => ws?.send(conversionMessage({ type: "setSkills", payload }));
export const socketShuffle = (payload: ShuffleMessageData["payload"]) => ws?.send(conversionMessage({ type: "shuffle", payload }));
export const socketViewDrawCards = (payload: ViewDrawCardsMessageData["payload"]) => ws?.send(conversionMessage({ type: "viewDrawCards", payload }));
export const socketViewDiscardCards = (payload: ViewDiscardCardsMessageData["payload"]) => ws?.send(conversionMessage({ type: "viewDiscardCards", payload }));
export const socketViewPlayerCards = (payload: ViewPlayerCardsMessageData["payload"]) => ws?.send(conversionMessage({ type: "viewPlayerCards", payload }));
export const socketGetDrawCards = (payload: GetDrawCardsMessageData["payload"]) => ws?.send(conversionMessage({ type: "getDrawCards", payload }));
export const socketGetDiscardCards = (payload: GetDiscardCardsMessageData["payload"]) => ws?.send(conversionMessage({ type: "getDiscardCards", payload }));
export const socketGetPlayerCards = (payload: GetPlayerCardsMessageData["payload"]) => ws?.send(conversionMessage({ type: "getPlayerCards", payload }));
export const socketTransferDrawCardsToDrawCards = (payload: TransferDrawCardsToDrawCardsMessageData["payload"]) => ws?.send(conversionMessage({ type: "transferDrawCardsToDrawCards", payload }));
export const socketTransferDrawCardsToDiscardCards = (payload: TransferDrawCardsToDiscardCardsMessageData["payload"]) => ws?.send(conversionMessage({ type: "transferDrawCardsToDiscardCards", payload }));
export const socketTransferDiscardCardsToDrawCards = (payload: TransferDiscardCardsToDrawCardsMessageData["payload"]) => ws?.send(conversionMessage({ type: "transferDiscardCardsToDrawCards", payload }));
export const socketTransferDiscardCardsToDiscardCards = (payload: TransferDiscardCardsToDiscardCardsMessageData["payload"]) => ws?.send(conversionMessage({ type: "transferDiscardCardsToDiscardCards", payload }));
export const socketTransferPlayerCardsToDrawCards = (payload: TransferPlayerCardsToDrawCardsMessageData["payload"]) => ws?.send(conversionMessage({ type: "transferPlayerCardsToDrawCards", payload }));
export const socketTransferPlayerCardsToDiscardCards = (payload: TransferPlayerCardsToDiscardCardsMessageData["payload"]) => ws?.send(conversionMessage({ type: "transferPlayerCardsToDiscardCards", payload }));
export const socketTransferPlayerCardsToPlayerHandCards = (payload: TransferPlayerCardsToPlayerHandCardsMessageData["payload"]) => ws?.send(conversionMessage({ type: "transferPlayerCardsToPlayerHandCards", payload }));
export const socketTransferDelayedCardsToPlayerDelayedCards = (payload: TransferDelayedCardsToPlayerDelayedCardsMessageData["payload"]) => ws?.send(conversionMessage({ type: "transferDelayedCardsToPlayerDelayedCards", payload }));
export const socketAddMaterielCards = (payload: AddMaterielCardsMessageData["payload"]) => ws?.send(conversionMessage({ type: "addMaterielCards", payload }));
export const socketAddDelayedCards = (payload: AddDelayedCardsMessageData["payload"]) => ws?.send(conversionMessage({ type: "addDelayedCards", payload }));
export const socketAddBuckleCards = (payload: AddBuckleCardsMessageData["payload"]) => ws?.send(conversionMessage({ type: "addBuckleCards", payload }));
export const socketSetDead = (payload: SetDeadMessageData["payload"]) => ws?.send(conversionMessage({ type: "setDead", payload }));
